package com.xxx.notice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxx.entity.Result;
import com.xxx.notice.client.ArticleClient;
import com.xxx.notice.client.UserClient;
import com.xxx.notice.domain.Article;
import com.xxx.notice.domain.Notice;
import com.xxx.notice.domain.NoticeFresh;
import com.xxx.notice.mapper.NoticeFreshMapper;
import com.xxx.notice.mapper.NoticeMapper;
import com.xxx.notice.service.NoticeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author blijojo
 */
@Service
@Slf4j
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice>
        implements NoticeService {

    /**
     * 文章类型在数据库中的值
     */
    public static final String ARTICLE_TYPE = "article";

    @Autowired
    private NoticeMapper noticeMapper;

    @Autowired
    private NoticeFreshMapper noticeFreshMapper;

    @Autowired
    private UserClient userClient;

    @Autowired
    private ArticleClient articleClient;

    @Override
    public void add(Notice notice) {
        notice.setState("0");
        notice.setCreateTime(new Date());
        noticeMapper.insert(notice);
    }

    @Override
    public Page<NoticeFresh> findNoticeFreshByUserIdWithPage(String userId, Integer page, Integer size) {
        Page<NoticeFresh> pageData = new Page<>();
        pageData.setCurrent(page);
        pageData.setSize(size);
        QueryWrapper<NoticeFresh> wrapper = new QueryWrapper<>();
        wrapper.eq("userId", userId);
        return noticeFreshMapper.selectPage(pageData, wrapper);
    }

    @Override
    public void deleteFresh(NoticeFresh noticeFresh) {
        QueryWrapper<NoticeFresh> wrapper = new QueryWrapper<>(noticeFresh);
        noticeFreshMapper.delete(wrapper);
    }

    @Override
    public Page<Notice> pageByCondition(Notice notice, Integer page, Integer size) {
        Page<Notice> pageData = new Page<>();
        pageData.setCurrent(page);
        pageData.setSize(size);
        QueryWrapper<Notice> wrapper = new QueryWrapper<>(notice);
        Page<Notice> noticePage = noticeMapper.selectPage(pageData, wrapper);
        List<Notice> noticeList = noticePage.getRecords();
        for (Notice n : noticeList) {
            getNoticeInfo(n);
        }
        noticePage.setRecords(noticeList);
        return noticePage;
    }

    @Override
    public Notice findById(String id) {
        Notice notice = noticeMapper.selectById(id);
        getNoticeInfo(notice);
        return notice;
    }

    @Override
    public Notice findByCondition(Map<String, Object> map) {
        List<Notice> notices = noticeMapper.selectByMap(map);
        return notices.get(0);
    }

    @Override
    public void addSysNotice(Article article) {
        //查询所有用户id
        Result result = userClient.findAllId();
        //遍历用户id
        if (result.getData() != null) {
            List ids = (List) result.getData();
            Notice notice;
            for (Object id : ids) {
                //初始化通知实体类
                notice = new Notice();
                notice.setReceiverId(id.toString());
                notice.setOperatorId(article.getUserId());
                notice.setAction("publish");
                notice.setTargetType("article");
                notice.setTargetId(article.getId());
                notice.setCreateTime(new Date());
                notice.setType("sys");
                notice.setState("0");
                //保存通知实体类
                noticeMapper.insert(notice);
                //保存新消息通知
                NoticeFresh noticeFresh = new NoticeFresh(id.toString(), notice.getId());
                noticeFreshMapper.insert(noticeFresh);
            }
        }
    }

    /**
     * 完善消息相关数据:OperatorName(操作者名称),TargetName(操作目标名称)
     *
     * @param notice 通知
     */
    private void getNoticeInfo(Notice notice) {
        //根据用户id获取用户昵称，封装到notice
        Result userResult = userClient.findById(notice.getOperatorId());
        //检查查询到的用户非空
        if (userResult.getData() != null) {
            HashMap userMap = (HashMap) userResult.getData();
            notice.setOperatorName(userMap.get("nickname").toString());
        }
        //如果操作目标类型为文章,获取文章标题，封装到notice
        if (ARTICLE_TYPE.equals(notice.getTargetType())) {
            Result articleResult = articleClient.findById(notice.getTargetId());
            log.info("result: {}", articleResult);
            //检查查询到的文章非空
            if (articleResult.getData() != null) {
                HashMap articleMap = (HashMap) articleResult.getData();
                notice.setTargetName(articleMap.get("title").toString());
            }

        }
    }
}




